/*****************************************************************


********************************************************************/
#include "main.h"
#ifdef QC20
	#include "ADAU1787_INIT_DATA.h"
#endif
#ifdef A1
	#include "A1-FILTER.h"
#endif
#ifdef F05
	#include "F05.h"
#endif
#ifdef QC25
	#include "QC25.h"
#endif
#define DEVICE_ADDR_IC_1_FAST                     0x50
typedef unsigned char      uint8_t;
extern unsigned boot_flag;


void MY_SIGMA_WRITE_REGISTER_BLOCK(int devAddress,int address,int length, uint8_t *pData)
{
	int m,k,i;
	if(length>64)
	{
		m = length/64;
		k = length%64;
		for(i=0;i<m;i++)
		{
			if(HAL_I2C_Mem_Write(&hi2c1, devAddress, address+(i<<6), I2C_MEMADD_SIZE_16BIT,(uint8_t*)(pData+(i<<6)),64, 0x10) != HAL_OK)
				boot_flag=1;
		}
		if(k>0)
		{
			if(HAL_I2C_Mem_Write(&hi2c1, devAddress, address+(m<<6), I2C_MEMADD_SIZE_16BIT,(uint8_t*)(pData+(m<<6)),k, 0x10) != HAL_OK)
				boot_flag=1;
		}
	}
	else if(length>0&&length<=64)
	{
		if(HAL_I2C_Mem_Write(&hi2c1, devAddress, address, I2C_MEMADD_SIZE_16BIT,(uint8_t*)pData,length, 0x10) != HAL_OK)
			boot_flag=1;
	}
	
}

void Parst_Adi1787_Eeprom_Data(uint8_t *data_ptr){
	#define end_self_boot 0
	#define write_multibyte 1
	#define delay_times 2
	#define no_operation 3
	#define wait_for_pll 4
	#define write_single_btye 5
	int current_pos = 0;
	unsigned short reg_addr;
	unsigned short value_len;
	boot_flag = 0;
	while (1)
	{
		if (current_pos >= DATA_SIZE)
		{
			break;
		}	
		switch (*(data_ptr + current_pos))
		{
		case end_self_boot:
			current_pos += DATA_SIZE;
			break;
		case write_multibyte:
			value_len = ((*(data_ptr + current_pos + 1))<< 8) + (*(data_ptr + current_pos + 2)) - 2;
			reg_addr = ((*(data_ptr + current_pos + 3))<< 8) + (*(data_ptr + current_pos + 4));
			MY_SIGMA_WRITE_REGISTER_BLOCK(DEVICE_ADDR_IC_1_FAST, reg_addr, value_len, (data_ptr + current_pos + 5));
			current_pos += (value_len + 5);
			break;
		case delay_times:
			HAL_Delay(20);
			current_pos += 3;
			break;
		case no_operation:
			current_pos += 1;
			break;	
		case wait_for_pll:
			current_pos += 1;
			break;	
		case write_single_btye:
			reg_addr = ((*(data_ptr + current_pos + 1))<< 8) + (*(data_ptr + current_pos + 2));
			MY_SIGMA_WRITE_REGISTER_BLOCK(DEVICE_ADDR_IC_1_FAST, reg_addr, 1, (data_ptr + current_pos + 3));
			current_pos += 4;
			break;	
		default:
			break;
		}
	}
	
};

void default_download_IC_1_Fast(){
	Parst_Adi1787_Eeprom_Data(self_boot_data);
}

void default_download_IC_1_Sigma(){}
